package com.hyyt.shopjsp.finance.controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hyyt.shopjsp.basic.pojo.BasicUsers;
import com.hyyt.shopjsp.basic.service.IBasicActorPurviewService;
import com.hyyt.shopjsp.common.controller.BaseController;
import com.hyyt.shopjsp.finance.pojo.ShopSupplierMallCoinDetail;
import com.hyyt.shopjsp.finance.service.IShopSupplierMallCoinDetailService;
import com.hyyt.shopjsp.util.common.EnumUtils;
import com.hyyt.shopjsp.util.common.Page;
import com.hyyt.shopjsp.util.common.ResultMsg;
import com.hyyt.shopjsp.util.common.Utils;
import com.hyyt.shopjsp.util.export.ExportExcel;

/**
 * Copyright (c) 2017 ShopJsp. All Rights Reserved.
 * ============================================================================
 * 版权所有 2011 - 今 北京华宇盈通科技有限公司，并保留所有权利。
 * ----------------------------------------------------------------------------
 * 提示：在未取得SHOPJSP商业授权之前，您不能将本软件应用于商业用途，否则SHOPJSP将保留追究的权力。
 * ----------------------------------------------------------------------------
 * 官方网站：http://www.shopjsp.com
 * ============================================================================
 * @author Wang Youchun on 2017/4/26 0024下午 15:43.
 */
@Controller
@RequestMapping("/back/supplierMallCoinDetail")
public class ShopSupplierMallCoinDetailController extends BaseController {
	private Logger log = Logger.getLogger(ShopSupplierMallCoinDetailController.class);
	private String basePath = "/back/shop/supplierMallCoinDetail/";
	
	@Resource
	private IShopSupplierMallCoinDetailService shopSupplierMallCoinDetailService;//供应商商城币收支明细service

	/**
	 * 跳转供应商金币收支明细页面
	 *
	 * @return 进入指定页面
	 */
	@RequestMapping("gotoSupplierMallCoinDetailPage")
	public String gotoSupplierMallCoinDetailPage(){
        return basePath + "supplierMallCoinDetailList";
	}

	/**
	 * 供应商金币明细列表
	 * @param page
	 * @param response
	 * @param request
	 */
	@RequestMapping(value="listSupplierMallCoinDetail",method=RequestMethod.POST)
	@ResponseBody
	public void listSupplierMallCoinDetail(String supplierLoginName, String shopName, String ordersNo, String registerDate, String registerDate2, Page page, HttpServletResponse response, HttpServletRequest request){
		Map<String, Object> params=new HashMap<>();
		if(Utils.objectIsNotEmpty(supplierLoginName)){
			params.put("supplierLoginName", supplierLoginName.trim());
		}
		if(Utils.objectIsNotEmpty(shopName)){
			params.put("shopName", shopName.trim());
		}
		if(Utils.objectIsNotEmpty(ordersNo)){
			params.put("ordersNo", ordersNo.trim());
		}
		if(Utils.objectIsNotEmpty(registerDate)){
			params.put("registerDate", registerDate);
		}
		if(Utils.objectIsNotEmpty(registerDate2)){
			params.put("registerDate2", registerDate2);
		}
		int totalRecordCount =shopSupplierMallCoinDetailService.countShopSupplierMallCoinDetail(params);
		params.put("sortColumns", "tradingTime desc");
		page.setPageInfo(page.getPageSize(), totalRecordCount, page.getCurrentPage());
		List<ShopSupplierMallCoinDetail> supplierMallCoinDetailList=shopSupplierMallCoinDetailService.queryShopSupplierMallCoinDetail(params, page.getPageRecordBeginIndex(), page.getPageSize());
		params.clear();
		params.put("total", totalRecordCount);// total键 存放总记录数，必须的
		params.put("rows", supplierMallCoinDetailList);// rows键 存放每页记录 list
		ResultMsg.create(params, response);  //把内容发送到页面
	}

	/**
	 * 导出店铺金币明细Excel
	 * @param response
	 * @param request
	 */
	@RequestMapping("exportSupplierMallCoinExcel")
	public void exportSupplierMallCoinExcel(String supplierLoginName, String shopName, String ordersNo, String registerDate, String registerDate2, HttpServletResponse response, HttpServletRequest request) throws UnsupportedEncodingException {
		Map<String, Object> params=new HashMap<>();
		if(Utils.objectIsNotEmpty(supplierLoginName)){
			//用于get请求强转，防止中文乱码
			supplierLoginName= new String(supplierLoginName.trim().getBytes("iso8859-1"),"UTF-8");
			params.put("supplierLoginName", supplierLoginName);
		}
		if(Utils.objectIsNotEmpty(shopName)){
			//用于get请求强转，防止中文乱码
			shopName= new String(shopName.trim().getBytes("iso8859-1"),"UTF-8");
			params.put("shopName", shopName);
		}
		if(Utils.objectIsNotEmpty(ordersNo)){
			params.put("ordersNo", ordersNo.trim());
		}
		if(Utils.objectIsNotEmpty(registerDate)){
			params.put("registerDate", registerDate);
		}
		if(Utils.objectIsNotEmpty(registerDate2)){
			params.put("registerDate2", registerDate2);
		}
		params.put("sortColumns", "tradingTime desc");
		List<ShopSupplierMallCoinDetail> supplierMallCoinDetailList=shopSupplierMallCoinDetailService.queryShopSupplierMallCoinDetail(params);
		SimpleDateFormat formatter = new SimpleDateFormat(EnumUtils.DataFormat.Y_M_D_H_m_s.getValue());
		List<Map<String,Object>> mallDetailList = new ArrayList<>();
		if(supplierMallCoinDetailList!=null&&supplierMallCoinDetailList.size()>0){
			for (ShopSupplierMallCoinDetail mallcoindetail : supplierMallCoinDetailList) {
				 Map<String,Object> map = new HashMap<>();
				 map.put("supplierLoginName", mallcoindetail.getSupplierLoginName());
				 map.put("shopName",mallcoindetail.getShopName());
				 map.put("loginName", mallcoindetail.getLoginName());
				 map.put("ordersNo",mallcoindetail.getOrdersNo());
				 switch (mallcoindetail.getType()){
				 	case 1:
				 		map.put("type","收入");
				 		break;
				 	case 2:
				 		map.put("type","支出");
				 		break;
				 	case 3:
				 		map.put("type","提现");
				 		break;
				 	case 4:
				 		map.put("type","提现失败");
				 		break;
				 }
				 map.put("tradMallCoin",mallcoindetail.getTradMallCoin());
				 map.put("totalInPut",mallcoindetail.getTotalInPut());
				 map.put("totalOutPut",mallcoindetail.getTotalOutPut());
				 map.put("tradingTime",formatter.format(mallcoindetail.getTradingTime()));
				 mallDetailList.add(map);
			}
		}
		String fileName = "店铺金币明细";
		String columnNames[] = {"店铺账号","店铺名称","购买会员名称","订单号","类型","交易金币","总收入","总支出","时间"};
		String keys[] = {"supplierLoginName","shopName","loginName","ordersNo","type","tradMallCoin","totalInPut","totalOutPut","tradingTime"};
		try {
            ExportExcel.download(fileName,mallDetailList,columnNames,keys,request,response);
        } catch (IOException e) {
            logger.error("导出Excel失败！");
        }
	}
}